package com.attendance.www.hr.exception;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.zhangkongli.frame.core.ResultMap;
import com.zhangkongli.frame.utils.JSONUtils;

public class ExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
//		request.setAttribute("exception", ex.toString());
//        request.setAttribute("exceptionStack", ex);
//        logs.error(ex.toString(), ex);
        try {
//        	ObjectMapper mapper = new ObjectMapper();
//            Writer strWriter = new StringWriter();
//            mapper.writeValue(strWriter, ResultMap.fault("00001", "系统发生错误"));
        	ex.printStackTrace();  
            StringWriter sw = new StringWriter();  
            ex.printStackTrace(new PrintWriter(sw, true));  
            String str = sw.toString();  
        	
        	Logger.getLogger(this.getClass()).error("错误信息" + "==>" + str);
        	
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=utf-8");
//			response.getWriter().print(strWriter.toString());
			response.getWriter().print(JSONUtils.getJSONString(ResultMap.fault("00001", "无法处理请求，请联系管理员！")));
		} catch (IOException e) {
			Logger.getLogger(this.getClass()).error("错误信息" + "==>" + e);
			e.printStackTrace();
		};
        return null;
	}
	
}
